var dataUrl = null;
var delPhotoId = "";
var delPhotoNum = 0;
$(function () {
    $(function () {
        // 绑定取消按钮事件
        $(".cancel").click(function () {
            parent.layer.close(parent.pageii);
        });
    })

    init();
    $("form").validate({
        submitHandler: function (form) {// 必须写在验证前面，否则无法ajax提交
            //jquery获取复选框值
            if (!checkForm()) {
                return;
            }
            $("#delPhotoId").val(delPhotoId);
            // var chk_value =[];//定义一个数组
            var index = layer.load(1, {shade: false});
            $("#submitButton").attr("disabled", "disabled");
            $(form).ajaxSubmit({
                dataType: "text",
                success: function (data) {
                    $("#submitButton").removeAttr("disabled");
                    layer.close(index);
                    if (data == "success") {
                        // layer.confirm('修改成功!是否关闭窗口?', function (index) {
                            // parent.grid.loadData();
                            parent.location.reload();
                            parent.layer.close(parent.pageii);
                            return false;
                        // });
                    } else {
                        layer.alert('修改失败！', 3);
                    }
                }
            });
        },
        errorPlacement: function (error, element) {// 自定义提示错误位置
            $(".l_err").css('display', 'block');
            // element.css('border','3px solid #FFCCCC');
            $(".l_err").html(error.html());
        },
        success: function (label) {// 验证通过后
            $(".l_err").css('display', 'none');
        }
    });
});
function init() {

}
function checkForm() {
    /* if ($("#checkItemId").val() == '') {
     layer.msg('请选择问题库');
     return false;
     }*/
    if ($("#closeMemo").val() == '') {
        layer.msg('请填写整改意见');
        return false;
    }
    var reformFileRecordSize=$("#reformFileRecordSize").val();
    var num=(parseInt(reformFileRecordSize)-delPhotoNum);
    if ($("#photoFile3").val() == '' && $("#photoFile1").val() == '' && $("#photoFile2").val() == ''&&num==0) {
        layer.msg('请上传至少一张检查意见图片');
        return false;
    }
    return true;
}
//删除
function deletePhoto(filePhotoId, type,n) {
    if (type == 1) {
        if(delPhotoId.indexOf(filePhotoId) == -1){
            if (delPhotoId != null && delPhotoId != "") {
                delPhotoId += ",";
            }
            delPhotoId += filePhotoId;
            delPhotoNum++;
        }
    }
    $("#photo" + filePhotoId).hide();
    $("#photoFile" + n).val("");
}
//替换
function updateFile(obj, filePhotoId,type) {
    var objUrl = getObjectURL(obj.files[0]);
    $("#photo" + filePhotoId).attr("src", objUrl);
    $("#photo" + filePhotoId).show();
    if (type == 1) {
        if(delPhotoId.indexOf(filePhotoId) == -1){
            if (delPhotoId != null && delPhotoId != "") {
                delPhotoId += ",";
            }
            delPhotoId += filePhotoId;
            delPhotoNum++;
        }
    }
}

function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}
function enlargePhoto(url) {
    if(url.src.indexOf( '/images/whitepanel_bg2.png')>-1){
        return
    }
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true,
        area: ['400px','auto'], //宽高
        // content: "<img  src=" + url.src + " width='400px' height='360px' />"
        content: "<img  src=" + url.src + " width='90%' style='width: 400px' />"
    });
}